<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Inter\Controller;
use OT\DataDictionary;

/**
 * 前台首页控制器
 * 主要获取首页聚合数据
 */
class CommentController extends InterController {

	   function __construct() {
		parent::__construct();
		    $this->zan = M("zanlist");
		    $this->comment = M("comment");
		
		 }

		public function index(){
            echo "Hello 你在干什么";
	}

    public function lists(){
        $uid = $_GET['uid'];
        $aid = $_GET['aid'];
        $rows = $_GET['rows'] ? $_GET['rows'] : 10;

				if($aid=='')
		{
            $status['msg'] = '缺少参数';
            $this->ajaxReturn($status);
		}
		$map['aid'] = $aid;

		$count      = $this->comment->where($map)->select();
        $Page       = new \Think\Page($count,$rows); 
		$map['pid'] = 0;
		$list = $this->comment->where($map)->order("zan desc")->limit($Page->firstRow.','.$Page->listRows)->select();
        if(!empty($list))
		{
			foreach($list as $key=>$val)
            {
				if($uid>0)
				{
					$list[$key]['status'] = 0;
					$count = $this->zan->where(array('uid'=>$uid,'cid'=>$val['id']))->count();
					if($count>0)
					{
					  $list[$key]['status'] = 1;
					}

				}
				$list[$key]['child_count'] = $this->comment->where(array('pid'=>$val['id']))->count();
				$list[$key]['children'] = $this->comment->where(array('pid'=>$val['id']))->order("zan desc")->limit('0,4')->select(); 
			}
		}
		$status['code'] = 0;
        $status['data'] = $list;
        $this->ajaxReturn($status);
		
    }

	public function getChild(){
        $uid = $_GET['uid']; 
        $cid = $_GET['cid'];
		$rows = $_GET['rows'] ? $_GET['rows'] : 10;

		if($cid=='')
		{
            $status['msg'] = '缺少参数';
            $this->ajaxReturn($status);
		}
		$map['id'] = $cid;
        $rs = $this->comment->where($map)->find();
		$rs['status'] = 0;
		 $zan= $this->zan->where(array('cid'=>$rs['id'],'uid'=>$uid))->count();
		if($zan>0)
		{
			$rs['status'] = 1;
		}
		if(!empty($rs)){
			$count      = $this->comment->where(array('pid'=>$cid))->count();
			$Page       = new \Think\Page($count,$rows);  
			$list = $this->comment->where(array('pid'=>$cid))->order("zan desc")->limit($Page->firstRow.','.$Page->listRows)->select();
			if(!empty($list))
			{
				foreach($list as $key=>$val)
				{ 
					if($uid>0)
					{
						
						$count = $this->zan->where(array('uid'=>$uid,'cid'=>$val['id']))->count();
						if($count>0)
						{
						  $list[$key]['status'] = 1;
						}else
						{
							$list[$key]['status'] = 0;
						}

					}
				
				}
			}
			$rs['children'] = $list;
				$status['code'] = 0;
				$status['data'] = $rs;
				$this->ajaxReturn($status);
	    }
				$status['code'] = 1;
        $status['msg'] = '获取列表异常';
        $this->ajaxReturn($status);
	}

	/*protected function getCommlist($map=array(),$parent_id = 0,&$result = array()){     

		$map['pid'] = $parent_id; 
		if($parent_id>0)
		{
			$limit='0,4';
		}
		$arr = $this->comment->where($map)->order("zan desc,addtime asc")->limit($limit)->select();   
		if(empty($arr)){
			return array();
		} 
		foreach($arr as $key=>$val)
		{
			$arr[$key]['status'] = 0;
			$count = $this->zan->where(array('uid'=>$map['uid'],'cid'=>$val['id']))->count();
			if($count>0)
			{
			  $arr[$key]['status'] = 1;
			}
		}
		foreach ($arr as $cm) {  
			$thisArr=&$result[];
			$map['uid'] = $cm["id"];
			$cm["children"] = $this->getCommlist($map,$cm["id"],$thisArr);    
			$thisArr = $cm;                                    
		}
		return $result;
   }*/

    public function addc(){
		$status['code'] = 1;
		$uid = $_POST['uid'];
        $content = $_POST['content'];
		$avatar = $_POST['avatar'];
		$nickname = $_POST['nickname'];
		$parent_id = $_POST['parent_id'] ? intval($_POST['parent_id']) : 0;
		$aid = $_POST['aid'];
		$pid = $_POST['pid'];
        $receive_id = $_POST['receive_id'];
				if($uid=='')
		{
            $status['msg'] = '您未登录';
            $this->ajaxReturn($status);
		}
				if($content=='')
		{
            $status['msg'] = '缺少参数';
            $this->ajaxReturn($status);
		}

						if($aid=='')
		{
            $status['msg'] = '缺少参数';
            $this->ajaxReturn($status);
		}

		
		
		$data['aid'] = $aid;
		$data['uid'] = $uid;
		$data['addtime'] = time();
		if($nickname)
		{
		  $data['nickname'] = $nickname;
	    }
		if($avatar)
		{
		   $data['avatar'] = $avatar;
		 }
		$data['content'] = $content;
		if($pid)
		{
          $data['pid'] = $pid;
		}
				if($parent_id>0)
		{
					

         $data['parent_id'] = $parent_id;

		   
		} 
		$id =  M("comment")->add($data); 
		if(intval($id)>0)
		{  
					if($receive_id>0){
							$nofi['uid'] = $receive_id;
							$nofi['content'] = $nickname.'@回复了你的评论';
							$nofi['addtime'] = $data['addtime'];
							$nofi['status'] = 0;  //  0 未读  1 已读
							$nofi['comment_id'] = $id; 
							M("nofify")->add($nofi);	   
						   $PushService = new \Think\Jpush(); 
					       $ar[] = $receive_id;
						   $receive = array('alias'=> $ar);
						   $arr = json_encode($data);
						   $ret = $PushService->send_pub($receive,$nickname.'@回复了你的评论','met',$arr); //echo $ret;
					 }
			    $rs = M("comment")->where(array('id'=>$id))->find();
		        $status['code'] = 0;
				$status['data'] = $rs;
                $status['msg'] = '发表成功';
				

                $this->ajaxReturn($status);
		}else
		{
				$status['code'] = 1;
                $status['msg'] = '发表失败';
                $this->ajaxReturn($status);
		}
		
    }

	public function notif(){
		$status['code'] = 1;
		$uid = $_POST['uid'];
		if($uid=='')
		{
            $status['msg'] = '缺少参数';
            $this->ajaxReturn($status);
		}
		$rs = M("nofify")->where(array('uid'=>$uid))->order("addtime desc")->select();
		if(!empty($rs))
		{
			foreach($rs as $key=>$val)
			{
				if($val['comment_id']>0)
				{
                  $rs[$key]['comment'] =  M("comment")->where(array('id'=>$val['comment_id']))->find();
				}
			}
		}
				$status['code'] = 0;
                $status['data'] = $rs;
                $this->ajaxReturn($status);
	}

	public function delno(){
		$status['code'] = 1;
		$id = $_POST['id'];
		if($id=='')
		{
            $status['msg'] = '缺少参数';
            $this->ajaxReturn($status);
		}
		$rs = M("nofify")->where(array('id'=>$id))->delete();
		if($rs !== false) {
				$status['code'] = 0;
                $status['msg'] = '删除成功';
                $this->ajaxReturn($status);
		} else {
			     $status['msg'] = '操作失败';
                $this->ajaxReturn($status);
		}

	}
//状态

	public function setstatus(){
		$status['code'] = 1;
		$id = $_POST['id'];
		if($id=='')
		{
            $status['msg'] = '缺少参数';
            $this->ajaxReturn($status);
		}
		  M("nofify")->where(array('id'=>$id))->setInc('status');
	
				$status['code'] = 0;
                $status['msg'] = '操作成功';
                $this->ajaxReturn($status);
		

	}
//未读消息

	public function weidu(){
		$status['code'] = 1;
		$uid = $_POST['uid'];
		if($uid=='')
		{
            $status['msg'] = '缺少参数';
            $this->ajaxReturn($status);
		}
		  $rs = M("nofify")->where(array('uid'=>$uid,'status'=>0))->count();
	
				$status['code'] = 0;
                $status['data'] = $rs;
                $this->ajaxReturn($status);	

	}


	public function dianzan(){
		$status['code'] = 1;
		$uid = $_POST['uid'];
		$avatar = $_POST['avatar'];
		$nickname = $_POST['nickname'];
		$aid = $_POST['aid'];
		$cid = $_POST['cid'];
				if($uid=='')
		{
            $status['msg'] = '缺少参数';
            $this->ajaxReturn($status);
		}


						if($aid=='')
		{
            $status['msg'] = '缺少参数';
            $this->ajaxReturn($status);
		}
								if($cid=='')
		{
            $status['msg'] = '缺少参数';
            $this->ajaxReturn($status);
		}

		$data['aid'] = $aid;
		$data['uid'] = $uid;
		$data['addtime'] = time();
		if($nickname)
		{
		  $data['nickname'] = $nickname;
	    }
				if($avatar)
		{
		$data['avatar'] = $avatar;
		 }

	    
	
          $data['cid'] = $cid;	
		$id = $this->zan->add($data);
		unset($data);
        $this->comment->where(array('id'=>$cid))->setInc('zan');
		if($id>0)
		{
		        $status['code'] = 0;
                $status['msg'] = '发表成功';
                $this->ajaxReturn($status);
		}else
		{
				$status['code'] = 1;
                $status['msg'] = '发表失败';
                $this->ajaxReturn($status);
		}
		
    }
}